package dev;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class SaveCollecteServlet extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("deprecation")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws IOException, ServletException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        String adresse= null, mail = null, jaune = null, bleu = null ;
        String trisac[] = null ;
        if(req.getParameter("adresse") != null && req.getParameter("user_mail") != null){
        	adresse = (String)req.getParameter("adresse") ;
        	mail = (String)req.getParameter("user_mail") ;
        	if(req.getParameter("jaune")!=null) jaune = req.getParameter("jaune") ;
        	if(req.getParameter("bleu")!=null) bleu = req.getParameter("bleu") ;
        	if(req.getParameter("trisac")!=null) trisac = (String[])req.getParameter("trisac").split(" ") ;
        }
        
        Key collecteKey = KeyFactory.createKey("email", user.getEmail());
    	Entity collecte_store = new Entity("Collecte", collecteKey);
        collecte_store.setProperty("adresse", adresse);
        collecte_store.setProperty("date", new Date());
        collecte_store.setProperty("user_email", mail);
        collecte_store.setProperty("user_name", user.getNickname());
        collecte_store.setProperty("jaune", jaune);
        collecte_store.setProperty("bleu", bleu);
        if(req.getParameter("trisac")!=null) { 
        	collecte_store.setProperty("trisac1", trisac[0]);
        	collecte_store.setProperty("trisac2", trisac[2]);
        }
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    	Query q = new Query("Collecte");
		q.addFilter("user_email", Query.FilterOperator.EQUAL, user.getEmail());
		PreparedQuery pq = datastore.prepare(q);

		ArrayList<Entity> addr_saved = new ArrayList<Entity>();
		for (Entity result : pq.asIterable()) {
			addr_saved.add(result);
		}
		if(addr_saved.size()>0) datastore.delete(addr_saved.get(0).getKey());
        datastore.put(collecte_store);
        resp.sendRedirect("clean_nantes?recherche="+adresse);
    }
    
  
}